﻿using BtLabelBLL;
using BtLabelModel;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 蓝牙注册工具.View
{
    /// <summary>
    /// BluetoothView.xaml 的交互逻辑
    /// </summary>
    public partial class BluetoothView : UserControl
    {
        public ViewModel.BtLogViewModel viewModel = new ViewModel.BtLogViewModel();
        private BtLabelBLL.BtUartBLL bTBLL = null;
        public SerialPort BtPort;
        bool loop = false;
        /// <summary>
        /// 蓝牙搜索的Mac列表
        /// </summary>
        public static List<BtLabelModel.DeviceModel> Macs = new List<BtLabelModel.DeviceModel>();
        public BluetoothView()
        {
            InitializeComponent();
            DataContext = viewModel;
            BtPort = BLL.PortBLL.serialPortBt;
            bTBLL = new BtUartBLL(BtPort, RichTextBoxBt);
            RichTextBoxBt.Document.Blocks.Remove(RichTextBoxBt.Document.Blocks.FirstBlock);
            GetMacs();
        }
        /// <summary>
        /// <summary>
        /// 扫描蓝牙
        /// </summary>
        /// <returns></returns>
        public void GetMacs()
        {
            Task.Run(async() =>
            {
                if (loop == true)
                {
                    return;
                }
                while (true)
                {
                    loop = true;
                    await Task.Delay(200);
                    try
                    {
                        lock (BtPort)
                        {
                            if (BtPort.IsOpen == false || BtPort.PortName != MainWindow.config.BtPortname)
                            {
                                viewModel.Port = BtPort.PortName;
                                BLL.PortBLL.InitPort(BtPort, MainWindow.config.BtPortname, 115200);
                                continue;
                            }
                        }
                        lock (Macs)
                        {
                            Task.Run(() =>
                            {
                                try
                                {
                                    Macs = bTBLL.GetBTDevices(byte.Parse(MainWindow.config.ScanDelay), (sbyte)int.Parse(MainWindow.config.dBm));
                                }
                                catch 
                                {
                                }

                            }).Wait();
                        }
                        Dispatcher.Invoke(() =>
                        {
                            viewModel.MacItems.Clear();
                            for (int i = 0; i < Macs.Count; i++)
                            {
                                ViewModel.BtLogViewModel.MacItem macItem = new ViewModel.BtLogViewModel.MacItem();
                                macItem.BT_MAC = Macs[i].Address;
                                macItem.dBm = Macs[i].DBm;
                                macItem.No = i;
                                viewModel.MacItems.Add(macItem);
                            }
                        });
                    }
                    catch (Exception ex)
                    {
                        UiBLL.RichTextBoxAppendText(RichTextBoxBt, Brushes.OrangeRed, ex.Message);
                    }
                }
            });
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }


    }
}
